<template>
	<view>
		<!-- 头部 -->
		<view class="title">
			<view class="title_address">
				<view class="title_box">
					<view class="title_icon">
						<text class="iconfont icon-dizhi" :style="'color:' + colors"></text>
					</view>
					<view class="center">
						<view class="left sele" v-if="selAddress.selectedAddress == ''">
							请选择你的地址
						</view>
						<view class="left" v-else>
							<view class="title_name">{{selAddress.selectedAddress}}{{selAddress.selectedMoreAddress}}
							</view>
							<view class="name-number">
								<text class="names">{{selAddress.receiverName}}</text>
								<text class="title-phones">{{selAddress.contactNumber}}</text>
							</view>
						</view>

						<view class="right">
							<u-icon name="arrow-right" color="#999" size="18" @tap="toOpen"></u-icon>


						</view>
					</view>



				</view>
			</view>

		</view>


		<view v-if="selectType" class="wallet_class" :class="type?'wallet_class_kq':'wallet_class_gb'">
			<!-- 新增地址 -->


			<view style="width: 750rpx;height:100vh;" @tap="toCancel"></view>
			<view class="wallet_content" :style="'height:'+ height" :class="type?'wallet_kq':'wallet_gb'">
				<!-- 头部 -->

				<view class="wallet_content_tb">
					<view class="cancel1" @tap="toCancel">
						<view class="cencel1_q">
							<uniIcons color="#333" size="32" type="closeempty"></uniIcons>
						</view>
					</view>
					<view class="tite">{{tite}}</view>
					<view class="operate1" @tap="toCancel">确认</view>
					<view class="add">
						<view class="text" @click="addAddress">
							新增地址
						</view>
					</view>



				</view>

				<view class="content">

					<scroll-view v-if="list" class="scroll-view" scroll-y="true">
						<view v-for=" (item,index) in list" :key="index" class="wallet_list"
							:class="{'wallet_listdj': selectedItemIndex === index}"" 
							 @click=" selectAddress(index)">

							<!-- 列表 -->
							<view class="myaddress">
								<view class="order_address">
									<view class="address_box">
										<view class="weizhi_icon">
											<text class="iconfont icon-dizhi" :style="'color:' + colors"></text>
										</view>
										<view>
											<view class="center">
												<view class="moren" v-if="item.isdefult == 1">
													<text class="iconfont icon-moren" :style="'color:' + colors"></text>
												</view>
												<view class="name">
													<text class="text1">{{item.name}}</text>
													<text class="phones">{{item.phone}}</text>
												</view>
												<view class="address_name">{{item.address}}{{item.detailAddress}}</view>
											</view>
										</view>
										<view class="caozuo">
											<view class="del" @click="delAddress(index)">
												<text class="iconfont icon-shanchu"></text>
												删除
											</view>
											<view class="edit" @click="editAddress(index)">
												<text class="iconfont icon-bianji"></text>
												编辑
											</view>
										</view>

									</view>
								</view>
							</view>

							<!-- 选择按钮 -->

						</view>
					</scroll-view>
					<view v-else style="text-align: center;color: #888;line-height: 100rpx;font-size: 26rpx;">暂无数据
					</view>

				</view>

			</view>

		</view>



	</view>
</template>

<script>
	import uniIcons from '@/components/uni-icons/uni-icons.vue';
	export default {
		name: 'jp-select',
		components: {
			uniIcons
		},
		props: {
			list: {
				type: Array //数据
			},
			shopAddress: {
				type: String,
			},

			tite: { //标题
				type: String,
				default: '请选择'
			},

			id: { //键
				type: String,
				default: 'id'
			},


			item: { //初始值
				type: [String, Object, Array],
				default: ''
			},
			amount: { //多选时选取最少数量   
				type: Number,
				default: 1
			},
			height: {
				type: String,
				default: '50vh'
			},
			color: { //主题颜色
				type: String,
				default: '#007AFF'
			},
			textColor: { //内容颜色
				type: String,
				default: '#333'
			},
			fontSize: { //内容大小
				type: String,
				default: '30rpx'
			},
			hideType: { //选中后是否立即关闭
				type: Boolean,
				default: true
			},
			num: {
				type: Number,
				default: 1
			},

		},
		data() {
			return {
				selectedItemIndex: '',
				selAddress: {
					receiverName: '',
					contactNumber: '',
					selectedAddress: '',
					selectedMoreAddress: '',
				},
				seleAddress: '',
				colors: '#fbbd08',
				selectType: false,
				type: true,
				style: {},
				textStyle: {},
				xzindex: -1,

			}
		},
		mounted() {

		},
		watch: {

		},
		methods: {
			addAddress() {

				uni.navigateTo({
					url: `/pages/address/address?action=add&shopAddress=${this.shopAddress}`
				});
			},

			selectAddress(index) {
				this.selectedItemIndex = index;
				this.$emit('selected-index-change', this.selectedItemIndex);
				this.selAddress.receiverName = this.list[index].name;
				this.selAddress.contactNumber = this.list[index].phone;
				this.selAddress.selectedAddress = this.list[index].address;
				this.selAddress.selectedMoreAddress = this.list[index].detailAddress;
				let addressString = {
					seladdress: this.selAddress.receiverName + ', ' + this.selAddress.contactNumber + ', ' + this
						.selAddress.selectedAddress + this.selAddress.selectedMoreAddress,
					sellat: this.list[index].latitude,
					sellon: this.list[index].longitude
				}
				console.log('shopAddress', this.shopAddress)

				this.$emit('data-updated', addressString);

			},

			editAddress(index) {


				const action = 'edit';
				const addressId = this.list[index].id;
				uni.navigateTo({
					url: `/pages/address/address?action=${action}&addressId=${addressId}&shopAddress=${this.shopAddress}`

				});


			},
			delAddress(index) {
				let addressIdd = this.list[index].id;
				console.log('addressIdd', addressIdd)


				uni.showModal({
					title: '提示',
					content: '确认要删除该地址吗?',
					confirmText: '删除',
					confirmColor: this.colors,
					success: (res) => {
						if (res.confirm) {
							uni.request({
								url: 'http://43.136.172.73/api/v1/address/delete?id=' + addressIdd,
								method: 'DELETE',
								header: {
									'Authorization': 'sfsdf',
								},


								success: (res) => {
									console.log('删除请求成功', res)
									uni.showToast({
										title: '删除地址成功',
										icon: 'none'
									})
									if (this.shopAddress == 'true') {
										uni.navigateTo({
											url: '/pages/purchase/purchase'
										})
									} else {
										uni.navigateTo({
											url: '/pages/appointment/appointment'
										})
									}

								},
								fail: (err) => {
									console.log('删除失败', err)
									uni.showToast({
										title: '删除失败',
										icon: 'none'
									})
								}
							});
						}
					}
				});
			},

			toOpen() {

				this.type = true
				this.selectType = true
			},
			toCancel() {
				let taht = this
				this.type = false
				this.$emit('toCancel');
				setTimeout(function clock() {
					taht.selectType = false

				}, 200)
			},
			toclose() {
				let taht = this
				this.type = false
				this.$emit('toCancel');
				taht.selectType = false
			},

		}
	}
</script>

<style lang="scss" scoped>
	@import "../../uni_modules/cc-addressSet/components/cc-addressSet/icon.css";

	.sele {
		font-size: 17px;
		color: #36B7AB;

	}


	.content {}

	.scroll-view {
		height: 800rpx;
		border: 1px solid #ccc;
		padding-bottom: 220rpx;
	}

	.add {
		background-color: #fff;
		width: 100%;
		height: 100rpx;
		font-size: 17px;
		display: flex;
		align-items: center;
		position: fixed;
		bottom: 0;
		z-index: 9999;

		.text {

			width: 300rpx;
			height: 80rpx;
			text-align: center;
			margin: 0 auto;
			border: 1px solid #FBC422;
			color: #FBC422;
			border-radius: 40rpx;
			line-height: 80rpx;

		}
	}

	.title {
		background-color: #fff;
		width: 100%;

		.title_box {
			padding: 0 40rpx;
			display: flex;
			align-items: center;

			.title_icon {
				display: flex;
				align-items: center;
				margin-right: 20rpx;
			}

			.center {
				display: flex;
				align-items: center;
				width: 100%;
				justify-content: space-between;

				.left {
					.title_name {
						font-size: 17px;
						font-weight: bold;
						margin-bottom: 10rpx;
					}

					.name-number {
						color: #D2D2D2;

						.names {
							margin-right: 10rpx;
						}
					}
				}
			}
		}
	}

	.select {
		display: flex;
		align-item: center;
		margin: 0;
		margin-left: 10px;
	}

	.myaddress {
		margin: auto;
		width: 94%;



		padding-bottom: 40upx;

	}

	.order_address {
		height: 180upx;
		width: 100%;
		background-color: #fff;
		border-radius: 10upx;
		position: relative;
		box-shadow: 0upx 0upx 10upx #ddd;
		margin-top: 7rpx;

	}

	.order_address image {
		width: 100%;
		height: 100%;
		display: block;
		position: absolute;
		top: 0;
		left: 0;
		z-index: 10;
	}

	.address_box {
		width: 100%;
		height: 100%;
		display: block;
		z-index: 10;
		box-sizing: border-box;
		padding: 20upx;
		display: flex;
		align-item: center;
	}

	.weizhi_icon text {
		font-size: 40upx;
		margin-left: 10upx;
	}

	.address_box .center {
		margin-left: 20upx;
	}

	.address_box .center .name {
		height: 60upx;
		line-height: 60upx;
	}

	.address_box .center .name .text1 {
		font-size: 26upx;
		font-weight: bold;
		color: #333;
		display: inline-block;
		margin-right: 20upx;
	}

	.phones {
		font-size: 24upx;
		color: #999;
		z-index: 0;
	}

	.address_box .address_name {
		font-size: 26upx;
		font-weight: bold;
		color: #333;
	}

	.noaddress {
		margin-left: 40upx;
		font-weight: bold;
		color: #333;
		font-size: 26upx;
	}

	.caozuo {
		position: absolute;
		right: 20upx;
		display: flex;
		top: 20upx;
	}

	.caozuo view {
		font-size: 24upx;
		color: #666;
		margin-left: 30upx;
	}

	.caozuo view text {
		font-size: 24upx;
	}

	.moren {
		position: absolute;
		top: 0;
		left: 0;
	}

	.moren text {
		font-size: 60upx;
	}


	@keyframes downIn {
		from {
			transform: translateY(100%);
			opacity: 1;
		}

		to {
			transform: translateY(0vh);
			opacity: 1;
		}
	}

	@keyframes downInClose {
		from {
			transform: translateY(0vh);
			opacity: 1;
		}

		to {
			transform: translateY(100%);
			opacity: 1;
		}
	}

	.wallet_class {
		position: fixed;
		z-index: 999;
		height: 100vh;
		width: 100%;
		top: 0;
		left: 0;
		background-color: rgba(0, 0, 0, .5);


		.wallet_kq {
			animation: downIn 0.3s;
			-webkit-animation: downIn 0.3s;
		}

		.wallet_gb {
			animation: downInClose 0.3s;
			-webkit-animation: downInClose 0.3s;
		}

		.wallet_content {
			position: fixed;
			bottom: 0;
			left: 0;
			border-top-left-radius: 20rpx;
			border-top-right-radius: 20rpx;
			width: 100%;
			background-color: #fff;



			.wallet_content_tb {
				display: flex;
				justify-content: space-between;
				height: 80rpx;
				font-size: 32rpx;
				border-bottom: #ccc solid 1px;
				width: 750rpx;
				line-height: 80rpx;


				.cancel0 {
					width: 100rpx;
					text-align: center;
					font-size: 30rpx;
					color: #333;
				}

				.cancel1 {
					width: 180rpx;
					font-size: 30rpx;
					display: flex;
					justify-content: flex-start;

					.cencel1_q {
						width: 100rpx;
						text-align: center;
						font-size: 30rpx;
						color: #333;
					}
				}

				.tite {
					color: #333;
				}

				.operate0 {
					width: 100rpx;
					text-align: center;
					font-size: 30rpx;
				}

				.operate1 {
					width: 180rpx;
					display: flex;
					justify-content: flex-end;
					margin-right: 40rpx;

					.operate1_q {
						width: 80rpx;
						text-align: center;
						font-size: 26rpx;
					}

					.operate1_p {
						width: 100rpx;
						text-align: center;
						font-size: 26rpx;
					}
				}

			}

			.wallet_list {
				height: 200rpx;
				display: flex;
				padding: 5rpx 20rpx;


			}
		}
	}

	.wallet_listdj {
		margin: 0 auto;
		margin-top: 10rpx;
		width: 96%;
		border-radius: 15rpx;
		padding-bottom: 20rpx;
		background-color: #FBC422;
		// animation: showPopup 0.2s linear both;
	}

	@keyframes showPopup {
		0% {
			opacity: 0;
			background-color: #d8d8d8;
		}

		100% {
			opacity: 1;
		}
	}
</style>